<?php

namespace App\Common\Cla;

use App\Common as com;
use App\Common\Cla\EChat as thisEChat;

class EChat extends com\Cla\ClaBase {

	/**
	 *
	 * @var string
	 */
	private string $uid;

	/**
	 * 通道sid
	 * @var string
	 */
	private string $channelSid;

	/**
	 *
	 * @var thisEChat\Server
	 */
	private thisEChat\Server $server;

	/**
	 * EChat constructor.
	 * @param string $channelSid 频道
	 */
	public function __construct( string $channelSid = 'default' ) {
		/*
		if( $channelSid === 'default' ){
			$this->server = thisEChat\Server::makeOfStatic();
		}else{
			$this->server = thisEChat\Server::makeOfNew();
		}
		*/
		$this->uid = com\Lib\Fun\Str::uuid( false );
		$this->server = thisEChat\Server::makeOfNew();
		//
		$this->setChannelSid( $channelSid );
		//
		parent::__construct();
	}

	/**
	 * 获取ChannelSid的前缀
	 * @return string
	 */
	public function getChannelSidPrefix(  ):string {
		return 'eCaht.';
	}


	public function getUid(  ):string {
		return $this->uid;
	}

	/**
	 * @param string $channelSid
	 * @return $this
	 */
	public function setChannelSid( string $channelSid = 'default' ):self {
		$this->channelSid = $channelSid;
		//
		return $this;
	}

	public function getChannelSid(  ) {
		return $this->channelSid;
	}

	/**
	 * @param string $mailId
	 * @param $body
	 * @param array $headers 头信息
	 */
	public function sendMail( string $mailId, $body, array $headers = [] ):void {
		$mailStruct = explode( '@', $mailId );
		//
		$headers['toMailId'] = $mailId;
		//
		$this->send( $mailStruct[2] ?? '', $mailStruct[1], $body, $headers );
		//

		$this->goRabbitMQ()->sendMail( $mailId, $body, $headers );
	}



	/**
	 * @param string $mailId
	 * @return EChat\Room
	 */
	public function joinRoomOfMail( string $mailId ):thisEChat\Room {
		$mailStruct = explode( '@', $mailId );
		if( count( $mailStruct ) < 2 ){
			throw com\Lib\Err\Exception::makeOfNew( "传入的mailId格式错误(mailId：{$mailId})，格式应为：A@B@C", com\Lib\Err\Code::$sysAssertError );
		}
		//
		return $this->joinRoom( $mailStruct[1], [
			'channelSid' => $mailStruct[0],
		] );
	}

	/**
	 * @param string|null $roomSid [null]随机创建
	 * @param array $params 参数数组
	 * ················· [可空](string)	$params['channelSid']	说明：通道id。[null][默认]使用默认通道
	 * ················· [可空/多选_A/必须](unknown_type)	$params['AAAAA']	说明：AAAAA
	 * @return EChat\Room
	 */
	public function joinRoom( ?string $roomSid = null, array $params = [] ):thisEChat\Room {
		return thisEChat\Room::makeOfNew( $this, $roomSid, $params );
	}

	/**
	 * @param array $roomSids
	 * @return EChat\Rooms
	 */
	public function joinRooms( array $roomSids ):thisEChat\Rooms {
		return thisEChat\Rooms::makeOfNew( $this, $roomSids );
	}

	/**
	 * @return EChat\JoinRoomPool
	 */
	public function newJoinRoomPool():thisEChat\JoinRoomPool {
		return thisEChat\JoinRoomPool::makeOfNew( $this );
	}

	/**
	 * @return com\Lib\Com\RabbitMQ
	 */
	public function goRabbitMQ(  ):com\Lib\Com\RabbitMQ {
		return $this->goPool( __FUNCTION__, function(){
		    return com\Lib\Com\RabbitMQ::newOfPoolOfDefault();
		} );
	}

	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * @param string $channelSid 频道
	 * @return $this
	 */
	public static function makeOfNew( string $channelSid = 'default' ):self {
		return self::make( $channelSid );
	}

	/**
	 * @param string $channelSid 频道
	 * @return $this
	 */
	public static function makeOfPools( string $channelSid = 'default' ):self {
		return com\Lib\StaticPool::getOrParams( static::class, $channelSid, function() use( $channelSid ) {
			return self::make( $channelSid );
		} );
	}

}
